import { makeAutoObservable } from 'mobx';
// 导入 API 和类型定义
import ollamaTestApi from '@/api/ollamaTestApi';
import { OllamaModel, SearchModel, ChatMessage } from '@/types/type';

class OllamaTestStore {
    // 模型列表状态
    models: OllamaModel[] = [];
    searchModels: SearchModel[] = [];
    selectedModel: string = 'deepseek-r1:latest';

    // 搜索相关状态
    modelTab: 'local' | 'search' = 'local';
    searchQuery: string = '';

    // 对话相关状态
    chatMessages: ChatMessage[] = [];
    userInput: string = '';

    // 加载状态
    isLoading: boolean = false;
    isInstalling: boolean = false;
    error: string | null = null;

    constructor() {
        makeAutoObservable(this);
    }

    // 加载本地模型列表
    async loadLocalModels() {
        this.isLoading = true;
        this.error = null;

        try {
            this.models = await ollamaTestApi.load_local_models();
        } catch (error) {
            this.error = error instanceof Error
                ? `获取模型列表失败: ${error.message}`
                : '获取模型列表失败: 发生未知错误';
            throw error;
        } finally {
            this.isLoading = false;
        }
    }

    // 搜索模型
    async searchModelList() {
        this.isLoading = true;
        this.error = null;
        this.searchModels = [];

        try {
            this.searchModels = await ollamaTestApi.search_models({ query: this.searchQuery });
        } catch (error) {
            this.error = error instanceof Error
                ? `搜索模型失败: ${error.message}`
                : '搜索模型失败: 发生未知错误';
            throw error;
        } finally {
            this.isLoading = false;
        }
    }

    // 安装模型
    async installModel(modelName: string) {
        this.isInstalling = true;
        this.error = null;

        try {
            await ollamaTestApi.install_model({ modelName });
            // 安装完成后刷新本地模型列表
            await this.loadLocalModels();
            this.modelTab = 'local'; // 切换回本地模型标签
        } catch (error) {
            this.error = error instanceof Error
                ? `安装模型失败: ${error.message}`
                : '安装模型失败: 发生未知错误';
            throw error;
        } finally {
            this.isInstalling = false;
        }
    }

    // 发送消息到模型
    async sendMessage() {
        if (!this.userInput.trim()) {
            throw new Error('请输入对话内容');
        }

        this.isLoading = true;
        this.error = null;

        // 添加用户消息到对话历史
        const newMessage: ChatMessage = { role: 'user', content: this.userInput };
        this.chatMessages.push(newMessage);
        this.userInput = '';

        try {
            // 调用API发送消息并获取回复
            const assistantReply = await ollamaTestApi.send_chat_message({ model: this.selectedModel, messages: this.chatMessages });
            // 添加助手回复到对话历史
            this.chatMessages.push(assistantReply);
        } catch (error) {
            this.error = error instanceof Error
                ? `调用失败: ${error.message}`
                : '调用失败: 发生未知错误';
            throw error;
        } finally {
            this.isLoading = false;
        }
    }

    // 清空聊天上下文
    clearChatMessages() {
        this.chatMessages = [];
    }

    // 设置选中的模型
    setSelectedModel(modelName: string) {
        this.selectedModel = modelName;
    }

    // 设置搜索关键词
    setSearchQuery(query: string) {
        this.searchQuery = query;
    }

    // 设置模型标签
    setModelTab(tab: 'local' | 'search') {
        this.modelTab = tab;
    }

    // 设置用户输入
    setUserInput(input: string) {
        this.userInput = input;
    }
}

// 导出单例
export default new OllamaTestStore();